import { create } from 'zustand';

// 预选的服饰信息
export interface PreselectDress {
  id: number;
  name?: string;
  dressType?: number;
  amount?: number;
  size?: string;
  pictureList?: string[];
  fileUrlList?: string[];
  queryData?: string;
  [key: string]: any; // 允许其他字段
}

// 预选的人员信息
export interface PreselectPerson {
  id: number;
  name?: string;
  bigType?: number; // 人员类型：化妆师、司仪、摄影、婚庆
  price?: number;
  phone?: string;
  bizData?: string;
  [key: string]: any; // 允许其他字段
}

interface PreselectState {
  dresses: PreselectDress[]; // 预选的服饰列表
  persons: PreselectPerson[]; // 预选的人员列表
}

interface PreselectActions {
  // 添加服饰
  addDress: (dress: PreselectDress) => void;
  // 移除服饰
  removeDress: (id: number) => void;
  // 添加人员
  addPerson: (person: PreselectPerson) => void;
  // 移除人员
  removePerson: (id: number) => void;
  // 清空所有
  clearAll: () => void;
  // 清空服饰
  clearDresses: () => void;
  // 清空人员
  clearPersons: () => void;
}

export type PreselectStore = PreselectState & PreselectActions;

export const usePreselectStore = create<PreselectStore>((set) => ({
  dresses: [],
  persons: [],
  
  addDress: (dress) => {
    set((state) => {
      // 检查是否已存在
      const exists = state.dresses.find((d) => d.id === dress.id);
      if (exists) {
        return state; // 已存在则不添加
      }
      return {
        dresses: [...state.dresses, dress],
      };
    });
  },
  
  removeDress: (id) => {
    set((state) => ({
      dresses: state.dresses.filter((d) => d.id !== id),
    }));
  },
  
  addPerson: (person) => {
    set((state) => {
      // 检查是否已存在
      const exists = state.persons.find((p) => p.id === person.id);
      if (exists) {
        return state; // 已存在则不添加
      }
      return {
        persons: [...state.persons, person],
      };
    });
  },
  
  removePerson: (id) => {
    set((state) => ({
      persons: state.persons.filter((p) => p.id !== id),
    }));
  },
  
  clearAll: () => {
    set({
      dresses: [],
      persons: [],
    });
  },
  
  clearDresses: () => {
    set({
      dresses: [],
    });
  },
  
  clearPersons: () => {
    set({
      persons: [],
    });
  },
}));

